﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.HelpDocument.HelpDocumentation;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.HelpDocument.HelpDocumentation;

namespace BCTMSRESTful.Controllers.Management.HelpDocument.HelpDocumentation
{
    /// <summary>
    /// 帮助
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class HelpDocumentationController : ManagementBaseController
    {
        private readonly IHelpDocumentationService helpDocumentationService;

        public HelpDocumentationController(IHelpDocumentationService helpDocumentationService)
        {
            this.helpDocumentationService = helpDocumentationService;
        }
        #region 检查帮助文档码是否已存在
        /// <summary>
        /// 检查帮助文档码是否已存在。
        /// </summary>
        /// <response code="200">返回帮助文档码是否已存在</response>
        /// <returns></returns>
        [HttpGet("[action]/{document_code}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult Exists([FromRoute(Name ="document_code")]string documentCode)
        {
            return this.Ok(this.helpDocumentationService.Exists(documentCode));
        }
        #endregion

        #region 添加帮助
        /// <summary>
        ///  添加帮助
        /// </summary>
        /// <param name="requestDto">帮助信息</param>
        /// <response code="201">添加帮助成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加帮助")]
        [ProducesResponseType(typeof(HelpDocumentationDto), 201)]
        public IActionResult Post(
            [FromBody]HelpDocumentationRequestDto requestDto)
        {
            var result = this.helpDocumentationService.Add(requestDto, this.Admin);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改帮助
        /// <summary>
        ///  修改帮助
        /// </summary>
        /// <param name="helpDocumentationId">帮助文档</param>
        /// <param name="requestDto">帮助信息</param>
        /// <response code="200">修改帮助成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{help_documentation_id}")]
        [Log("修改帮助")]
        [ProducesResponseType(typeof(HelpDocumentationDto), 200)]
        public IActionResult Put([FromRoute(Name = "help_documentation_id")]long helpDocumentationId, [FromBody]HelpDocumentationRequestDto requestDto)
        {
            HelpDocumentationPutDto putDto = requestDto.As<HelpDocumentationPutDto>();
            putDto.HelpDocumentationId = helpDocumentationId;
            return StatusCode((int)HttpStatusCode.OK, this.helpDocumentationService.Update(putDto, this.Admin));
        }
        #endregion

        #region 删除帮助
        /// <summary>
        ///  删除帮助
        /// </summary>
        /// <param name="helpDocumentationId">帮助文档</param>
        /// <response code="204">删除帮助成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{help_documentation_id}")]
        [Log("删除帮助")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "help_documentation_id")]long helpDocumentationId)
        {
            this.helpDocumentationService.Delete(helpDocumentationId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取帮助
        /// <summary>
        ///  获取帮助
        /// </summary>
        /// <param name="documentCode">帮助文档码</param>
        /// <response code="200">获取帮助</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{document_code}")]
        [ProducesResponseType(typeof(HelpDocumentationDto), 200)]
        public IActionResult Get([FromRoute(Name = "document_code")]string documentCode)
        {
            return StatusCode((int)HttpStatusCode.OK, this.helpDocumentationService.Get(documentCode));
        }
        #endregion

        #region 获取帮助列表
        /// <summary>
        ///  获取帮助列表
        /// </summary>
        /// <param name="helpDocumentCategoryId">帮助文档分类Id</param>
        /// <response code="200">获取帮助列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]/{category_id}")]
        [ProducesResponseType(typeof(HelpDocumentationDto), 200)]
        public IActionResult List([FromRoute(Name ="category_id")]long helpDocumentCategoryId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.helpDocumentationService.GetList(helpDocumentCategoryId));
        }
        #endregion

        #region 获取帮助（分页）
        /// <summary>
        ///  获取帮助（分页）
        /// </summary>
        /// <param name="helpDocumentCategoryId">帮助文档分类Id</param>
        /// <param name="searchKey">搜索内容</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取帮助（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<HelpDocumentationDto>), 200)]
        public IActionResult Query(
            [FromRoute(Name = "category_id")]long? helpDocumentCategoryId,
            [FromRoute(Name = "search_key")]string searchKey,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.helpDocumentationService.Query(helpDocumentCategoryId,searchKey,start, end, sortName, ascending, index, size));
        }
        #endregion
    }
}

